package com.ruoyi.web.controller.training;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.pdf.PdfExporter;
import com.ruoyi.training.domain.TrainPlanPaper;
import com.ruoyi.training.domain.req.CommitPaperProcessReq;
import com.ruoyi.training.domain.req.PlanPaperReq;
import com.ruoyi.training.service.ITrainPlanPaperService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 培训计划试卷Controller
 *
 * @author wng
 * @date 2024-08-04
 */
@RestController
@RequestMapping("/training/paper")
public class TrainPlanPaperController extends BaseController {
    @Resource
    private ITrainPlanPaperService trainPlanPaperService;

    /**
     * 查询培训计划试卷列表
     */
    @PreAuthorize("@ss.hasPermi('training:paper:list')")
    @GetMapping("/list")
    public TableDataInfo list(TrainPlanPaper trainPlanPaper) {
        startPage();
        LoginUser loginUser = getLoginUser();
        if (loginUser.getUserId() != 1 && trainPlanPaper.getDeptId() == null) {
            trainPlanPaper.setDeptId(loginUser.getDeptId());
        }
        List<TrainPlanPaper> list = trainPlanPaperService.selectTrainPlanPaperList(trainPlanPaper);
        return getDataTable(list);
    }

    /**
     * 导出培训计划试卷列表
     */
    @PreAuthorize("@ss.hasPermi('training:paper:export')")
    @Log(title = "培训计划试卷", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TrainPlanPaper trainPlanPaper) throws Exception{
        List<TrainPlanPaper> list = trainPlanPaperService.selectTrainPlanPaperList(trainPlanPaper);
//        ExcelUtil<TrainPlanPaper> util = new ExcelUtil<TrainPlanPaper>(TrainPlanPaper.class);
//        util.exportExcel(response, list, "培训计划试卷数据");
        PdfExporter<TrainPlanPaper> util = new PdfExporter<>();
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "attachment; filename=export.pdf");
        util.exportListToPdf(list,response.getOutputStream());
    }

    /**
     * 获取培训计划试卷详细信息
     */
    @PreAuthorize("@ss.hasPermi('training:paper:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(trainPlanPaperService.selectTrainPlanPaperById(id));
    }

    /**
     * 根据培训计划查询试卷详情
     */
    @PreAuthorize("@ss.hasPermi('training:paper:planPaper')")
    @GetMapping(value = "/planPaper")
    public AjaxResult planPaper(PlanPaperReq req) {
        LoginUser loginUser = getLoginUser();
        req.setUserId(loginUser.getUserId());
        return success(trainPlanPaperService.planPaper(req));
    }

    /**
     * 做一题保存一题,实时保存做题进度
     */
    @PreAuthorize("@ss.hasPermi('training:paper:commitPaperProcess')")
    @PostMapping(value = "/commitPaperProcess")
    public AjaxResult commitPaperProcess(@RequestBody CommitPaperProcessReq req) {
        LoginUser loginUser = getLoginUser();
        req.setUserId(loginUser.getUserId());
        trainPlanPaperService.commitPaperProcess(req);
        return success();
    }

    /**
     * 提交考试
     */
    @PreAuthorize("@ss.hasPermi('training:paper:commitExam')")
    @PostMapping(value = "/commitExam")
    public AjaxResult commitExam(@RequestBody CommitPaperProcessReq req) {
        LoginUser loginUser = getLoginUser();
        req.setUserId(loginUser.getUserId());
        trainPlanPaperService.commitExam(req);
        return success();
    }


    /**
     * 新增培训计划试卷
     */
    @PreAuthorize("@ss.hasPermi('training:paper:add')")
    @Log(title = "培训计划试卷", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TrainPlanPaper trainPlanPaper) {
        LoginUser loginUser = getLoginUser();
        trainPlanPaper.setCreateBy(String.valueOf(loginUser.getUserId()));
        trainPlanPaper.setDeptId(loginUser.getDeptId());
        return toAjax(trainPlanPaperService.insertTrainPlanPaper(trainPlanPaper));
    }

    /**
     * 修改培训计划试卷
     */
    @PreAuthorize("@ss.hasPermi('training:paper:edit')")
    @Log(title = "培训计划试卷", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TrainPlanPaper trainPlanPaper) {
        LoginUser loginUser = getLoginUser();
        trainPlanPaper.setUpdateBy(String.valueOf(loginUser.getUserId()));
        return toAjax(trainPlanPaperService.updateTrainPlanPaper(trainPlanPaper));
    }

    /**
     * 删除培训计划试卷
     */
    @PreAuthorize("@ss.hasPermi('training:paper:remove')")
    @Log(title = "培训计划试卷", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(trainPlanPaperService.deleteTrainPlanPaperByIds(ids));
    }
}
